<?php
 include "counter2.php";

  if (!$area = $counter->get_input('area', 'area', 'get|post')) {
      $area = "All";
      $join = "?";
  } else {
      $join = "?area=$area&";
  }
  $query = mysql_query('SELECT * FROM places WHERE name=\''. $area .'\'');
  if (!$query) {
      print mysql_error();
      die('Query failed');
  }
  if (mysql_num_rows($query) > 0) {
      $place = mysql_fetch_array($query);
      $name = $place['name'];
      $longname = $place['longname'];
  }
pagestart();
print "<html>";
pagehead(_("Area Rank Report for ") . $name);

print "<body bgcolor=\"white\">\n";
pagetop(_("Linux density for places in ") . _($longname));
 
print _("This view gives you the ranking of density for places within ") . _($longname) . _(" as of ") . strftime("%a %b %d %H:%M:%S %Y %Z") .
"\n<p>\n" .
_("You can click on each entry to get a list of places within that place.") .
"\n<p>\n" .
_("To get population figures added, contact the maintainer for the area.") . "\n<br>\n" .
_("The maintainer for this area is ") . findmaintainer($place) .
"\n<p>\n" .
_("There are ") . $place['users'] . _(" persons registered.") .
"\n<p>\n";

  if (!$orderby = $counter->get_input('orderby', 'alpha', 'get')) { $orderby = 'userdensity'; }
  if (!$limit = $counter->get_input('limit', 'number', 'get')) { $limit = 1; }

  if ($orderby == 'longname') {
      $desc = 'asc';
  } else {
      $desc = 'desc';
  }
  $query = mysql_query('SELECT name, longname, users, population, IFNULL(users/population,0) AS userdensity FROM places WHERE within=\''. $place['f_key'] .'\' AND users >= '. $limit .' ORDER BY '. $orderby .' '. $desc);
  if ($query == 0) {
      print('</table>MySQL error: '. mysql_errno() .':'. mysql_error());
      die('Query for count failed');
  }
  $rank = 1;
  while ($arr = mysql_fetch_array($query)) {
      if ($rank == 1) {

print "\n<a name=\"table\"></a>" .
"<h2>" . _("Where Linux users in ") . _($longname) . _(" live") . "</h2>\n" .
_("The table is sorted by ");

          if ($orderby == 'userdensity') {
              print _("number of users divided by population") . "\n";
          } else {
              print $orderby;
          }
          if ($limit > 1) {
              print _("Places with less than ") . $limit . _(" users are not listed") . "\n";
          }

print "\n<table border=4>" .
   "\n<tr>" .
       "\n<th>" . _("No") . "</th>" .
       "\n<th>" . _("ID") . "</th>" .
       "\n<!-- note - RFC 2396 specifies that fragment follows query part -->";
print "\n<th><a href=\"" . $_SERVER['PHP_SELF'].$join . "orderby=longname#table\">" . _("Place") . "</a></th>" .
       "\n<th><a href=\"" . $_SERVER['PHP_SELF'].$join . "orderby=users#table\">" . _("Pers") . "</a></th>" .
       "\n<th><a href=\"" . $_SERVER['PHP_SELF'].$join . "orderby=userdensity#table\">" . _("P/Mpop") . "</a></th>" .
       "\n<th><a href=\"" . $_SERVER['PHP_SELF'].$join . "orderby=population#table\">" . _("People") . "</a></th>" .
   "\n</tr>";

      }

print "\n<tr align=right>" .
       "\n<td>" . $rank . "</td>" .
       "\n<td align=left>" . $arr['name'] . "</td>" .
       "\n<td align=left><a href=\"" . $_SERVER['PHP_SELF'] . "?area=" . urlencode($arr['name']); if ($orderby != 'userdensity') { print "&orderby=". $orderby; } print "\">". $arr['longname'] ."</a>" . "</td>" .
       "\n<td>"; printf("%d", $arr['users']); print "</td>" .
       "\n<td>"; if (isset($arr['userdensity'])) { printf("%5.2f", $arr['userdensity']); } else { print(_("Unknown")); } print "</td>" .
       "\n<td>"; if ($arr['population'] >= 1) { printf("%6.1fM", $arr['population']); } elseif ($arr['population'] > 0) { printf("%6.0fK", $arr['population'] * 1000); } else { print(_("Unknown")); } print "</td>" .
   "\n</tr>";

      ++$rank;
  }
  if ($rank > 1) {
      print "</table>";
  }
pagebottom();
print "\n</body>\n</html>";
?>
